
import cloud from '@lafjs/cloud'
import nw from "nw-lafjs";
import {fail, ok} from "@/utils/responseUtil";
const _ = cloud.database().command
export default async function (ctx: FunctionContext) {
  let body = ctx.body
  /**
   * {
   *     "_id": "",
   *     "activityName": "",
   *     "activityType": "FULL_REDUCTION",
   *     "activityDesc": "",
   *     "startTime": "",
   *     "endTime": ""
   * }
   */
  if (nw.util.isNull(body._id)){
    return fail("数据不合法")
  }
  let dataJson = {}
  if (!nw.util.isNull(body.activityName)){
    //校验名字是否重复
    let count = await nw.db.count({
      dbName:"activityInfo",
      whereJson:{name:_.eq(body.activityName),deleted:0,_id:_.neq(body._id)}
    })
    if (count>0){
      return fail("活动名字重复，无法修改")
    }
    // @ts-ignore
    dataJson.activityName = body.activityName
  }
  if (!nw.util.isNull(body.activityType)){
    // @ts-ignore
    dataJson.activityType = body.activityType
  }
  if (!nw.util.isNull(body.activityDesc)){
    // @ts-ignore
    dataJson.activityDesc = body.activityDesc
  }

  if (!nw.util.isNull(body.startTime)){
    // @ts-ignore
    dataJson.startTime = new Date(body.startTime)
  }

  if (!nw.util.isNull(body.endTime)){
    // @ts-ignore
    dataJson.endTime = new Date(body.endTime)
  }
  let count = await nw.db.update({
    dbName:"activityInfo",
    whereJson:{_id:body._id},
    dataJson:dataJson
  })
  if (count===0){
    return fail("修改失败")
  }
  return ok("修改成功")
}
